
#include <QtGui>
#include <QPainter>
#include <QVector>

#include "algorytma.h"
#include "fileloader.h"
#include "cubicwindow.h"
#include "vesselpoint.h"

    AlgorytmA::AlgorytmA(QWidget * parent, int maxX, int maxY, int maxZ) : QDialog(parent){
        setupUi(this, maxX, maxY, maxZ);
        connect(zSlider, SIGNAL(valueChanged(int)), this, SLOT(updateZ(int)));
        fl = FileLoader::getInstance();
        mouseX = 0;
        mouseY = 0;
        zPosition = 1;
        xPosition = new QVector<int>(maxZ);
        yPosition = new QVector<int>(maxZ);
        for(int i=0; i<maxZ; i++){
            (*xPosition)[i] = -1;
            (*yPosition)[i] = -1;    
        }
        updateZ(1);
    }

    void AlgorytmA::setupUi(QWidget *mainField, int maxX, int maxY, int maxZ)
    {
        if (mainField->objectName().isEmpty())
            mainField->setObjectName(QString::fromUtf8("mainField"));
            
        int w = maxX;
        if(w < 160) w = 160;
        int h = maxY;
        if(h < 160) h = 180;
                
        mainField->resize(w+30, h+130);
        
        zLabel = new QLabel(mainField);
        zLabel->setObjectName(QString::fromUtf8("zLabel"));
        zLabel->setGeometry(QRect(20, 10+h, w, 25));
        zSlider = new QSlider(mainField);
        zSlider->setObjectName(QString::fromUtf8("zSlider"));
        zSlider->setGeometry(QRect(20, 45+h, w, 25));
        zSlider->setOrientation(Qt::Horizontal);
        zSlider->setMinimum(1);
        zSlider->setMaximum(maxZ);
        
        ZImage = new QLabel(mainField);
        ZImage->setObjectName(QString::fromUtf8("ZImage"));
        ZImage->setGeometry(QRect(10, 10, w, h));
               
        mainField->setWindowTitle("Algorytm");
        
        zLabel->setText("Z position: 1");

        QMetaObject::connectSlotsByName(mainField);

    } // setupUi

    void AlgorytmA::updateZ(int val){
        zLabel->setText(QString("Z position: ")+QString::number(zSlider->value()));
        QImage *img = fl->getZImage(0, 0, zSlider->value());
        if((*xPosition)[val-1]!= -1){
            img->setPixel((*xPosition)[val-1], (*yPosition)[val-1], 255);
            img->setPixel((*xPosition)[val-1]+1, (*yPosition)[val-1], 255);
            img->setPixel((*xPosition)[val-1], (*yPosition)[val-1]+1, 255);
            img->setPixel((*xPosition)[val-1]+1, (*yPosition)[val-1]+1, 255);
        }
        ZImage->setPixmap(QPixmap::fromImage(*img,Qt::ColorOnly));
    }
    
    void AlgorytmA::mousePressEvent(QMouseEvent * event){
        (*xPosition)[zSlider->value()-1] = event->x()-10;
        (*yPosition)[zSlider->value()-1] = event->y()-10;
        zPosition = zSlider->value()-1;
    }
